/*******************************************************************************
 * 表单初始化
 ******************************************************************************/
function generateTextInput(td,config,dataJson){
	var input	=	$("<input type='text' class='input-text' id='infos_"+config.field+"' name='infos."+config.field+"'/>");
	if(undefined != config.field && "" != config.field){
		input.attr("id","infos_"+config.field);
		input.val(dataJson[config.field]);
	}else if(undefined != config.name && "" != config.name){
		input.attr("id","infos_"+config.name);
		input.attr("name",config.name);
		input.val(dataJson[config.name]);
	}
	if(0!=td.width()){
		input.width(td.width()-4);	
	}
	
	
	if(undefined != config.style && "" != config.style){
		input.attr("style",config.style);
	}
	
	if(undefined != config.className && "" != config.className){
		input.attr("class",config.className);
	}
	
	if(undefined != config.placeholder){
		input.attr("placeholder",config.placeholder);
	}
	var auth	;
	if(typeof(findFieldAuth) != "undefined"){
		auth	=	findFieldAuth(config.field);
	}
	if(undefined == auth){
		auth	=	"010";
	}
	
	if("100" == auth){
		input.attr("disabled","disabled");	
	}else if("010" == auth){
		
	}else if("001" == auth){
		input.css("display","none");
	}
	
	td.html("");
	td.append(input);
}

/*******************************************************************************
 * 大文本
 ******************************************************************************/
function generateTextAreaInput(td,config,dataJson){
	var input	=	$("<textarea id='infos_"+config.field+"' name='infos."+config.field+"'/>");
	input.width(td.width()-4);
	if(undefined != config.rows){
		input.attr("rows",config.rows);
	}
	if(undefined != config.rows){
		input.attr("cols",config.cols);
	}
	
	if(undefined != config.style && "" != config.style){
		input.attr("style",config.style);
	}
	
	if(undefined != config.className && "" != config.className){
		input.attr("class",config.className);
	}
	
	if(undefined != config.field && "" != config.field){
		input.attr("id","infos_"+config.field);
		input.val(dataJson[config.field]);
	}else if(undefined != config.name && "" != config.name){
		input.attr("id","infos_"+config.name);
		input.attr("name",config.name);
		input.val(dataJson[config.name]);
	}
	td.html("");
	td.append(input);
}

/*******************************************************************************
 * 大文本
 ******************************************************************************/
function generateFatTextAreaInput(td,config,dataJson){
	var input	=	$("<textarea id='infos_"+config.field+"' name='infos."+config.field+"'/>");
	input.width(td.width()-4);
	
	if(undefined != config.field && "" != config.field){
		input.attr("id","infos_"+config.field);
		input.val(dataJson[config.field]);
	}else if(undefined != config.name && "" != config.name){
		input.attr("id","infos_"+config.name);
		input.attr("name",config.name);
		input.val(dataJson[config.name]);
	}
	
	td.html("");
	td.append(input);
	
	var id	=	"infos_"+config.field;
	CKEDITOR.replace(id,
      	{height: config.height, width: config.width, pages:true,subtitle:true,textareaid:id,module:'content',catid:'208',
		flashupload:true,
		alowuploadexts:'',allowbrowser:'1',allowuploadnum:'10',authkey:'95b29f6dfd9ec758c7366822c6429c72',
		filebrowserUploadUrl : '/uploadFile?type=1',
      	toolbar :
		[
	['Maximize','Find','Replace','SelectAll','RemoveFormat','Image','-'],	
	   	['Bold','Italic','Underline','Strike','Outdent','Indent','-'],
        ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','Link','Unlink','-'],
	['Anchor','Table','SpecialChar',
	'Styles','Format','Font','FontSize'],['TextColor','BGColor'],
	['attachment'],
]
});
}

function generateSelectInput(td,config,dataJson){
	var input	=	$("<select id='infos_"+config.field+"' name='infos."+config.field+"'/>");
	input.width(td.width()-4);
	if(undefined != config.style && "" != config.style){
		input.attr("style",config.style);
	}
	
	if(undefined != config.className && "" != config.className){
		input.attr("class",config.className);
	}
	
	
	if(undefined != config.querySql && undefined != config.codeField && 
		undefined != config.titleField && "" != config.querySql && "" != config.codeField && 
		"" != config.titleField){
		/*
		 * $.get(application+'/browse/form/compent/showSelectDatas.action?fromTable='+
		 * config.fromTable+'&codeField='+config.codeField+'&titleField='+config.titleField+
		 * "&condition="+config.condition,function(data){ input.html(data);
		 * if(undefined != config.field && "" != config.field){
		 * input.attr("id","infos_"+config.field);
		 * input.val(dataJson[config.field]); }else if(undefined != config.name && "" !=
		 * config.name){ input.attr("id","infos_"+config.name);
		 * input.attr("name",config.name); input.val(dataJson[config.name]); }
		 * td.html(""); td.append(input); });
		 */
		
		$.post(application+'/browse/form/compent/showSelectDatas.action', 
				{ config: $.toJSON(config)},
				function (data, textStatus){        
					    // data 可以是 xmlDoc, jsonObj, html, text, 等等.
					    // this;
					    // 这个Ajax请求的选项配置信息，请参考jQuery.get()说到的this
					input.html(data);
					if(undefined != config.field && "" != config.field){
						input.attr("id","infos_"+config.field);
						input.val(dataJson[config.field]);
					}else if(undefined != config.name && "" != config.name){
						input.attr("id","infos_"+config.name);
						input.attr("name",config.name);
						input.val(dataJson[config.name]);
					}
					td.html("");
					td.append(input);      
				},"html");
		
	}else{
		var optionContent	=	config.options;
		if(undefined != optionContent){
			var options	=	optionContent.split("\n");
			for(var x=0; x<options.length; x++){
				var strs	=	options[x].split("|");
				var opt		=	$("<option/>");
				opt.html(strs[0]);
				opt.val(strs[1]);
				input.append(opt);
			}
		}
		if(undefined != config.field && "" != config.field){
			input.attr("id","infos_"+config.field);
			input.val(dataJson[config.field]);
		}else if(undefined != config.name && "" != config.name){
			input.attr("id","infos_"+config.name);
			input.attr("name",config.name);
			input.val(dataJson[config.name]);
		}
		td.html("");
		td.append(input);
	}
}

function generateRadioInput(td,config,dataJson){

	td.html("");
	var optionContent	=	config.options;
	if(undefined != optionContent){
		var options	=	optionContent.split("\n");
		for(var x=0; x<options.length; x++){
			var strs	=	options[x].split("|");
			var check		=	$("<input class='radioInput' type='radio' name='infos."+config.field+"' id='infos."+config.field+"_"+x+"'/>");
			var label	=	$("<label for='infos."+config.field+"_"+x+"'>"+strs[0]+"</label>");
			check.val(strs[1]);
			if(x == 0){
				check.attr("checked","checked");
			}
			
			if(undefined != config.style && "" != config.style){
				check.attr("style",config.style);
			}
			
			if(undefined != config.className && "" != config.className){
				check.attr("class",config.className);
			}
			td.append(check);
			td.append(label);
		}
	}
	if(undefined != dataJson[config.field]){
		$("input[name='infos."+config.field+"'][value='"+dataJson[config.field]+"']").attr("checked",true);
	} 
}

function generateCheckInput(td,config,dataJson){
	td.html("");
	var optionContent	=	config.options;
	if(undefined != optionContent){
		var options	=	optionContent.split("\n");
		for(var x=0; x<options.length; x++){
			var strs	=	options[x].split("|");
			var check		=	$("<input class='"+config.field+" checkInput' type='checkbox' name='infos."+config.field+"' id='infos."+config.field+"_"+x+"'/>");
			var label	=	$("<label for='infos."+config.field+"_"+x+"'>"+strs[0]+"</label>");
			check.val(strs[1]);
			
			if(undefined != config.style && "" != config.style){
				check.attr("style",config.style);
			}
			
			if(undefined != config.className && "" != config.className){
				check.attr("class",config.className);
			}
			td.append(check);
			td.append(label);
		}
	}
	
	if(undefined != dataJson[config.field]){
		$("input[name='infos."+config.field+"'][value='"+dataJson[config.field]+"']").attr("checked",true);
	}
}

function generateDateInput(td,config,dataJson){
	var input	=	$("<input type='text' class='input-text' id='infos_"+config.field+"' name='infos."+config.field+"'/>");
	input.width(td.width()-4);
	
	if(undefined != config.field && "" != config.field){
		input.attr("id","infos_"+config.field);
		input.val(dataJson[config.field]);
	}else if(undefined != config.name && "" != config.name){
		input.attr("id","infos_"+config.name);
		input.attr("name",config.name);
		input.val(dataJson[config.name]);
	}
	td.html("");
	td.append(input);
	input.datebox({   
	});
}

function generateTimeInput(td,config,dataJson){
	var input	=	$("<input type='text' class='input-text' id='infos_"+config.field+"' name='infos."+config.field+"'/>");
	input.width(td.width()-4);
	input.height(30);
	if(undefined != config.field && "" != config.field){
		input.attr("id","infos_"+config.field);
		input.val(dataJson[config.field]);
	}else if(undefined != config.name && "" != config.name){
		input.attr("id","infos_"+config.name);
		input.attr("name",config.name);
		input.val(dataJson[config.name]);
	}
	td.html("");
	td.append(input);
	input.datetimebox({   
	});
}

function generateAutoCompleteInput(td,config,dataJson){
	var input	=	$("<input type='text' class='input-text' id='infos_"+config.field+"' name='infos."+config.field+"'/>");
	input.width(td.width()-4);
	input.val(dataJson[config.field]);
	
	if(undefined != config.style && "" != config.style){
		input.attr("style",config.style);
	}
	
	if(undefined != config.className && "" != config.className){
		input.attr("class",config.className);
	}
	
	td.html("");
	td.append(input);
	if(undefined != config.querySql && undefined != config.codeField && 
		undefined != config.titleField){
		
		
		input.autocomplete(application+'/browse/form/compent/showAutoDatas.action',
			{
				dataType: "json",
				type:"post",
				max: 0,
				extraParams:{config:$.toJSON(config)},
				parse: function(data) {
					return $.map(data, function(row) {
						return {
							data: row,
							value: row.name,
							result: row.name
						}
					});
				},
				formatItem: function(item) {
					return format(item);
				}
			}
		);
	}
}

function generateComTreeInput(td,config,dataJson){
	var input	=	$("<input type='text' class='input-text' id='infos_"+config.field+"' name='infos."+config.field+"'/>");
	input.width(td.width()-4);
	if(undefined != dataJson[config.field] && ""!=dataJson[config.field]){
		input.val(dataJson[config.field]);
	}
	
	if(undefined != config.style && "" != config.style){
		input.attr("style",config.style);
	}
	
	if(undefined != config.className && "" != config.className){
		input.attr("class",config.className);
	}
	
	td.html("");
	td.append(input);
	if(undefined != config.querySql && undefined != config.codeField && 
		undefined != config.titleField && undefined != config.parentField){
		
		input.combotree({
			
		});
		
		$.post(application+'/browse/form/compent/showComTreeDatas.action', 
			{ config: $.toJSON(config)},
			function (data, textStatus){        
				    // data 可以是 xmlDoc, jsonObj, html, text, 等等.
				    // this;
				    // 这个Ajax请求的选项配置信息，请参考jQuery.get()说到的this
				if(undefined != data){
					input.combotree("loadData",data);
				}        
			},"json");
	}
}

function generateLabelInput(td,config,dataJson){
	var input	=	$("<div />");
	input.width(td.width()-4);
	input.html(config.content);
	td.css("line-height",config.lineHeight+"px");
	td.css("text-indent",config.textIndet+"em");
	if(undefined != config.id){
		input.attr("id",config.id);
	}
	td.html("");
	
	if(undefined != config.style && "" != config.style){
		input.attr("style",config.style);
	}
	
	if(undefined != config.className && "" != config.className){
		input.attr("class",config.className);
	}
	td.append(input);
}

function generateFJInput(td,config,dataJson){
	var input	=	$("<input type='hidden' />");
	input.attr("id","infos_"+config.field);
	input.width(td.width()-4);
	
	var div	=	$("<div />");
	div.attr("id","fjinfos_"+config.field);
	
	var value	=	dataJson[config.field];
	if(undefined == value || ""==value){
		div.append("无附件(<a href='javascript:fjManage(\""+config.field+"\")'>附件管理</a>)");
	}else{
		input.val(value);
		$.get(application+"/browse/fj/showFJState.action?bd_key="+value,function(data){
			div.append(data+"个文件");
			div.append("(<a href='javascript:fjManage(\""+config.field+"\")'>附件管理</a>)");
		});
		
	}
	
	td.css("line-height",config.lineHeight+"px");
	td.css("text-indent",config.textIndet+"em");
	td.html("");
	td.append(input);
	td.append(div);
}
// 附件管理
function fjManage(compentID){
	var bd_key	=	$("#infos_"+compentID).val();
	if("" == bd_key){
		$.get(application+"/browse/fj/fjGenerate.action",function(data){
			bd_key	=	data;
			$("#infos_"+compentID).val(bd_key);
			var url	=	application+"/browse/fj/showFJDatas.action?bd_key="+bd_key;
		    result=	window.showModalDialog(url,
			     		"", "dialogHeight:550px;dialogWidth:810px;center:yes;status:no;help:no;scroll:no");
			fjRefresh(bd_key,compentID);
		});
	}else{
		$("#infos_"+compentID).val(bd_key);
		var url	=	application+"/browse/fj/showFJDatas.action?bd_key="+bd_key;
		var result=	window.showModalDialog(url,
			     	"", "dialogHeight:550px;dialogWidth:810px;center:yes;status:no;help:no;scroll:no");
		fjRefresh(bd_key,compentID);
	}
}	

function fjRefresh(bd_key,compentID){

	var div	=	$("#fjinfos_"+compentID);
	div.html("");
	$.get(application+"/browse/fj/showFJState.action?bd_key="+bd_key,function(data){
		div.append(data+"个文件");
		div.append("(<a href='javascript:fjManage(\""+compentID+"\")'>附件管理</a>)");
	});
}

function format(mail) {
	return mail.name + " &lt;" + mail.to + "&gt";
}

function findFormDatas(form){
		
	var str = form.serialize();
		// str= cardSelectDate=3&startdate=2012-02-01&enddate=2012-02-04
	var ob= strToObj(str);
	return ob;
}
	
function strToObj(str){
	str = str.replace(/&/g,"','");
	str = str.replace(/=/g,"':'");
	str = "({'"+str +"'})";
	obj = eval(str); 
	return obj;
}

/*******************************************************************************
 * 保存下级表单
 ******************************************************************************/
function saveChildForm(parentForm,ppvalues){
	
	var forms	=	$("form");
	var childForms	=	new Array();
	
	for(var x=0; x<forms.length; x++){
		var form	=	$(forms[x]);
		var parent	=	form.attr("parentForm");
		if(parentForm == parent){
			childForms[childForms.length]	=	form;
		}
	}
	
	for(var x=0; x<childForms.length; x++){
		var childForm	=	childForms[x];
		var id	=	childForm.attr("selfForm");
		var saveFunction	=	"saveForm_"+id+"()";
		eval(saveFunction);
	}
}

var costomCompentJss	=	new Array();

function generateCostomCompent(inputConfig,td,config,dataJson){
	
	var inputType	=	config.inputType;
	var jsPath		=	config.jsPath;
	
	if(-1 == costomCompentJss.indexOf(inputType)){
		loadResource(application +"/"+ jsPath, "js");
	}
	
	var comConfig	=	{
		"dataJson":dataJson,
	};
	var str	=	"td."+inputType+"(comConfig)";
	console.info(str);
	eval(str);
	return td.data("costomInput");
	//td.pageLink(comConfig);
	//td.append(inputType);
}